<template>
  <div id="address-list" class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="handleFilter()" size="small">查询搜索</el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetFilter()"
          size="small"
        >重置</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="收件人/会员昵称：">
            <el-input v-model="listQuery.keyword" class="input-width" placeholder="收件人/会员昵称" clearable></el-input>
          </el-form-item>
          <el-form-item label="手机号：">
            <el-input v-model="listQuery.phone" class="input-width" placeholder="手机号" clearable></el-input>
          </el-form-item>
        </el-form>
        <!--disabled值动态显示，默认为true,当选中复选框后值为false-->
        <el-button type="danger" @click="deleteBatch" :disabled="this.multipleSelection.length === 0">批量删除</el-button>
      </div>
    </el-card>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      ref="addressTable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>

      <el-table-column label="编号" prop="id" sortable="custom" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货人姓名" min-width="80px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员昵称" min-width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货地址" min-width="300px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.province +" "+ row.city+" " + row.district+" " + row.address}}</span>
        </template>
      </el-table-column>
      <el-table-column label="邮政编号" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.zipCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="100px" class-name="operation">
        <template slot-scope="{row}">
          <el-link type="warning" @click="handUpdate(row.id)">修改</el-link>
          <el-link type="danger" @click="handleDelete(row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageIndex"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <address-handle is-detail ref="detailPage" />
    <address-handle is-update ref="updatePage" @change="getList" />
  </div>
</template>

<script>
import waves from "@/directive/waves";
import Pagination from "@/components/Pagination";

import AddressHandle from "./components/address-handle";

import addressApi from "@/api/system/address-api";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  keyword: null,
  phone: null
};

export default {
  name: "AdderssList",
  components: { AddressHandle, Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      sortColumn: "id",
      sortAsc: true,
      multipleSelection: [],
      listQuery: {
        pageIndex: 1,
        pageSize: 10,
        keyword: null,
        phone: null,
        memberId: this.$route.query.memberId,
      },
      
      tableColumnChecked: null,
      createTimeRange: null,
      downloadLoading: false
    };
  },
  created() {
    this.setDefaultSort();
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      console.log(this.listQuery)
      addressApi.getPageList(this.listQuery).then(response => {
        this.list = response.data.records;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
   
    handleFilter() {
      this.listQuery.pageIndex = 1;
      this.listQuery.keyword = this.listQuery.keyword;
      this.getList();
    },
    /* 重置查询 */
    handleResetFilter() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getList();
    },

    handleClear() {
      this.handleFilter();
    },
    setDefaultSort() {
      /*  设置默认排序 */
      this.listQuery.pageSorts = [
        {
          column: this.sortColumn,
          asc: this.sortAsc
        }
      ];
    },

    handDetail(id) {
      alert(id);
      this.$nextTick(() => {
        this.$refs.detailPage.handle(id);
      });
    },
    handUpdate(id) {
      this.$nextTick(() => {
        this.$refs.updatePage.handle(id);
      });
    },
    /* 单个删除 */
    handleDelete(id) {
      this.$confirm("您确定要删除这个收货地址 ?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        addressApi.delete(id).then(response => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.handleFilter();
        });
      });
    },
    //操作多选
   handleSelectionChange(val) {
        this.multipleSelection = val;
      },
    /* 批量删除 */
    deleteBatch() {
      var ids = [];
      for(let i = 0;i<this.multipleSelection.length;i++){
        ids.push(this.multipleSelection[i].id);
      }
      this.$confirm("您确定要批量删除这些地址 ?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        addressApi.detdeleteBatchIds(ids).then(response => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.handleFilter();
        });
      });
    }
  }
};
</script>

<style lang="scss">
#address-list .el-table th,
.el-table td {
  padding: 6px 0px;
}

#address-list .filter-container {
  padding-bottom: 5px;
}

#address-list .filter-container .tree-select-item {
  width: 120px;
  display: inline-block;
  vertical-align: top;
  margin-right: 4px;
}

#address-list .input-with-select {
  vertical-align: top;
  width: 505px;
  margin-right: 4px;
}

#address-list .input-with-select .el-select .el-input {
  width: 120px;
}

#address-list .input-with-select .el-input-group__prepend {
  background-color: #fff;
}

#address-list .filter-item {
  margin-right: 4px;
}

#address-list .el-table__body .operation .cell {
  text-align: center;
}
</style>
